import { defineStore } from "pinia";

export const useComWinStore = defineStore("com-win", {
  state: () => {
    return {
      wins: [],
      currentTab: "fun",
      tabDict: [
        { name: "功能", val: "fun" },
        { name: "地图", val: "map" },
        { name: "待办", val: "todo" },
        { name: "圈子", val: "circle" },
        { name: "知识", val: "knowledge" },
      ],
    };
  },
  actions: {
    /**
     * 打开窗口 - 存在旧的应用则打开之前窗口
     * @param {窗口应用} app
     */
    async openWin(app, param) {
      // 关闭所有窗口
      this.wins.forEach((win) => (win.show = false));
      // 查找当前打开窗口
      let currWin = this.wins.find((win) => win.code === app.code);
      // 如果当前应用不存在或者带参，则打开新窗口
      if (!currWin || param) {
        app.param = param;
        this.openNewWin(app);
      } else {
        currWin.show = true;
      }
    },
    /**
     * 打开新窗口
     * @param {窗口应用} app
     */
    async openNewWin(app) {
      let newWin = {
        title: app.name,
        code: app.code,
        full: app.full,
        param: app.param,
        show: true,
      };
      this.wins.push(newWin);
      return newWin;
    },
    /**
     * 点击窗口tab - 本来打开就隐藏，否则打开
     * @param {窗口应用} currWin
     */
    selectWin(currWin) {
      if (this.wins.find((win) => win.show === true) !== currWin) {
        this.wins.forEach((win) => (win.show = false));
        currWin.show = true;
      } else {
        currWin.show = false;
      }
    },
  },
});
